<?php
class Categorybrand extends AppModel {
	var $name = 'Categorybrand';
	var $displayField = 'brand_id';
	var $validate = array(		
		'category_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'brand_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		)
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

        var $belongsTo = array(
		'Category' => array(
			'className' => 'Category',
			'foreignKey' => 'category_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Brand' => array(
			'className' => 'Brand',
			'foreignKey' => 'brand_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function total_brands($id = null){
		$this->recursive=-1;
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'brand');
		return $this->find('count',array('conditions'=>$conditions));
		
	}
	function total_styles($id = null){
		$this->recursive=-1;		
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'style');
		return $this->find('count',array('conditions'=>$conditions));
	}
	function total_conditions($id = null){
		$this->recursive=-1;
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'condition');
		return $this->find('count',array('conditions'=>$conditions));
	}
	function fetchbrand($id=null,$id2=null){
		$this->recursive = 0;
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'brand');
                //$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'brand','Brand.brand_description LIKE' => $this->params['url']['q'] . '%');
		$order=array('Brand.brand_description' => 'asc');
		return $this->find('all',array('conditions'=>$conditions,'order'=>$order));
	}
	function fetchstyle($id=null,$id2=null){
		$this->recursive = 0;		
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'style');
		$order=array('Brand.brand_description' => 'asc');
		return $this->find('all',array('conditions'=>$conditions,'order'=>$order));
		
	}
	function fetchcondition($id=null,$id2=null){
		$this->recursive = 0;		
		$conditions=array('Categorybrand.category_id'=>$id,'Categorybrand.brand_type'=>'condition');
		$order=array('Brand.brand_description' => 'asc');
		return $this->find('all',array('conditions'=>$conditions,'order'=>$order));
	}
	function brandindex($id1=null,$id2=null){
		$parent_id=$id1;
		$child_categories=$this->Category->viewAll($parent_id);
		$cats[$parent_id]=$parent_id;
		foreach($child_categories as $child_category){
			$cats[$child_category['id']]=$child_category['id'];
		}
		//Fetch Brands list
		$this->recursive=1;
		$fields=array('Brand.id','Brand.brand_description');
		if($id2=="1")
		$conditions=array('Categorybrand.category_id'=>$cats,'Categorybrand.brand_type'=>'brand','Brand.brand_description REGEXP'=>"^[0-9]");
		else
		$conditions=array('Categorybrand.category_id'=>$cats,'Categorybrand.brand_type'=>'brand','Brand.brand_description LIKE'=>$id2."%");
		$order=array('Brand.brand_description' => 'asc');
		$group=array('Brand.brand_description');
		
		$brands=$this->find('all',array('group'=>$group,'fields'=>$fields,'conditions'=>$conditions,'order'=>$order));
		return $brands;
	}
}
?>